﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.InternalServices;
using Microsoft.Win32;

namespace SoftwareConsulting.BI11.Studio.InternalServices
{    
    /// <summary>
    /// Класс восстановителя формы
    /// </summary>
    public class FormRestorer
    {
        /// <summary>
        /// Загружает форму
        /// </summary>
        /// <param name="form"></param>
        public static void Load(Form form)
        {
            object rec = null;
            RegistryKey readKey = Registry.CurrentUser.OpenSubKey("Software\\SoftwareConsulting\\BI-11\\WindowsStates\\" + form.Name);
            if (readKey != null)
            {
                rec = readKey.GetValue("left");
                if (rec != null)
                {
                    try
                    {
                        form.Left = (int)rec;
                    }
                    catch (InvalidCastException ex)
                    {
                        Trace.StartBlock("Registry record reading failed");
                        Trace.Message(ex.Message + " " + rec.ToString());
                        Trace.EndBlock();
                    }
                }
                rec = readKey.GetValue("top");
                if (rec != null)
                {
                    try
                    {
                        form.Top = (int)rec;
                    }
                    catch (InvalidCastException ex)
                    {
                        Trace.StartBlock("Registry record reading failed");
                        Trace.Message(ex.Message + " " + rec.ToString());
                        Trace.EndBlock();
                    }
                }
                rec = readKey.GetValue("width");
                if (rec != null)
                {
                    try
                    {
                        form.Width = (int)rec;
                    }
                    catch (InvalidCastException ex)
                    {
                        Trace.StartBlock("Registry record reading failed");
                        Trace.Message(ex.Message + " " + rec.ToString());
                        Trace.EndBlock();
                    }
                }
                rec = readKey.GetValue("height");
                if (rec != null)
                {
                    try
                    {
                        form.Height = (int)rec;
                    }
                    catch (InvalidCastException ex)
                    {
                        Trace.StartBlock("Registry record reading failed");
                        Trace.Message(ex.Message + " " + rec.ToString());
                        Trace.EndBlock();
                    }
                }
                rec = readKey.GetValue("state");
                if (rec != null)
                {
                    try
                    {
                        form.WindowState = (FormWindowState)rec;
                    }
                    catch (InvalidCastException ex)
                    {
                        Trace.StartBlock("Registry record reading failed");
                        Trace.Message(ex.Message + " " + rec.ToString());
                        Trace.EndBlock();
                    }
                }
                readKey.Close();
            }            
        }

        /// <summary>
        /// Сохраняет форму
        /// </summary>
        /// <param name="form"></param>
        public static void Save(Form form)
        {
            RegistryKey saveKey = Registry.CurrentUser.CreateSubKey("Software\\SoftwareConsulting\\BI-11\\WindowsStates\\" + form.Name);
            saveKey.SetValue("left", form.Left, RegistryValueKind.DWord);
            saveKey.SetValue("top", form.Top, RegistryValueKind.DWord);
            saveKey.SetValue("width", form.Width, RegistryValueKind.DWord);
            saveKey.SetValue("height", form.Height, RegistryValueKind.DWord);
            saveKey.SetValue("state", form.WindowState, RegistryValueKind.DWord);
            saveKey.Close();
        }
    }

    /// <summary>
    /// Класс формы, умеющий запоминать и восстанавливать свое состояние
    /// </summary>    
    public class RestorableForm : Form
    {        
        /// <summary>
        /// Обрабатывает создание восстанавливаемой формы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RestoreableForm_Load(object sender, EventArgs e)
        {
            FormRestorer.Load(this);
        }

        /// <summary>
        /// Обрабатывает закрытие восстанавливаемой формы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RestoreableForm_FormClosed(object sender, EventArgs e)
        {
            FormRestorer.Save(this);
        }

        /// <summary>
        /// Конструктор формы
        /// </summary>
        public RestorableForm():base()
        {
            Load += new EventHandler(RestoreableForm_Load);
            FormClosed += new FormClosedEventHandler(RestoreableForm_FormClosed);
        }        
    }
}
